package de.duester.genealogy.model.service;

import java.util.ArrayList;
import java.util.List;

import de.duester.genealogy.Utility;
import de.duester.genealogy.model.Date;
import de.duester.genealogy.model.Document;
import de.duester.genealogy.model.Event;
import de.duester.genealogy.model.EventAttribute;
import de.duester.genealogy.model.EventCode;
import de.duester.genealogy.model.EventDate;
import de.duester.genealogy.model.EventDocument;
import de.duester.genealogy.model.EventPlace;
import de.duester.genealogy.model.EventString;
import de.duester.genealogy.model.Person;
import de.duester.genealogy.model.PersonEvent;
import de.duester.genealogy.model.Place;
import de.duester.genealogy.model.RoleCode;
import de.duester.genealogy.model.dao.EventCodeDao.Properties;
import de.greenrobot.dao.DaoException;

public class EventService {

	public static List<Person> getPersons(Event event) {
		List<Person> list = new ArrayList<Person>();
		try {
			for (PersonEvent pe : event.getPersonEvents())
				if (!list.contains(pe.getPerson()))
					list.add(pe.getPerson());
		} catch (DaoException e) {
		}
		return list;
	}

	public static List<Person> getPersons(Event event, String roleCode) {
		List<Person> list = new ArrayList<Person>();
		try {
			for (PersonEvent pe : event.getPersonEvents())
				if (pe.getRoleCode().equals(roleCode) && !list.contains(pe.getPerson()))
					list.add(pe.getPerson());
		} catch (DaoException e) {
		}
		return list;
	}

	public static Person getPerson(Event event, String roleCode) {
		List<Person> list = getPersons(event, roleCode);
		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	public static List<Document> getDocuments(Event event) {
		List<Document> list = new ArrayList<Document>();
		try {
			for (EventDocument ed : event.getEventDocuments())
				if (!list.contains(ed.getDocument()))
					list.add(ed.getDocument());
		} catch (DaoException e) {
		}
		return list;
	}

	public static List<Document> getDocuments(Event event, int type) {
		List<Document> list = new ArrayList<Document>();
		List<Document> documents = getDocuments(event);
		for (Document document : documents)
			if (document.getType().equals(type))
				list.add(document);
		return list;
	}

	public static List<Place> getPlaces(Event event) {
		List<Place> list = new ArrayList<Place>();
		try {
			for (EventPlace ep : event.getEventPlaces())
				if (!list.contains(ep.getPlace()))
					list.add(ep.getPlace());
		} catch (DaoException e) {
		}
		return list;
	}

	public static Place getPlace(Event event, String roleCode) {
		try {
			for (EventPlace ep : event.getEventPlaces())
				if (ep.getRoleCode().equals(roleCode))
					return ep.getPlace();
		} catch (DaoException e) {
		}
		return null;
	}

	public static List<Date> getDates(Event event) {
		List<Date> list = new ArrayList<Date>();
		try {
			for (EventDate ed : event.getEventDates())
				if (!list.contains(ed.getDate()))
					list.add(ed.getDate());
		} catch (DaoException e) {
		}
		return list;
	}

	public static Date getDate(Event event, String roleCode) {
		try {
			for (EventDate ed : event.getEventDates())
				if (ed.getRoleCode().equals(roleCode))
					return ed.getDate();
		} catch (DaoException e) {
		}
		return null;
	}

	public static List<String> getStrings(Event event) {
		List<String> list = new ArrayList<String>();
		try {
			for (EventString es : event.getEventStrings())
				if (!list.contains(es.getString()))
					list.add(es.getString());
		} catch (DaoException e) {
		}
		return list;
	}

	public static String getString(Event event, String roleCode) {
		try {
			for (EventString es : event.getEventStrings())
				if (es.getRoleCode().equals(roleCode))
					return es.getString();
		} catch (DaoException e) {
		}
		return null;
	}

	public static String getType(String code) {
		List<EventCode> ec = Utility.getDaoSession().getEventCodeDao().queryBuilder()
				.where(Properties.Code.eq(code)).list();
		if (ec.size() > 0)
			return ec.get(0).getType();
		else
			return null;
	}

	public static List<EventAttribute> getAttributes(String eventTypeCode) {
		return Utility
				.getDaoSession()
				.getEventAttributeDao()
				.queryBuilder()
				.where(de.duester.genealogy.model.dao.EventAttributeDao.Properties.EventTypeCode
						.eq(eventTypeCode)).list();
	}

	public static RoleCode getRole(String roleCode) {
		List<? extends RoleCode> rc = Utility
				.getDaoSession()
				.getPersonRoleCodeDao()
				.queryBuilder()
				.where(de.duester.genealogy.model.dao.PersonRoleCodeDao.Properties.Code
						.eq(roleCode)).list();
		if (rc.size() > 0)
			return rc.get(0);
		else {
			rc = Utility
					.getDaoSession()
					.getRoleCodeDao()
					.queryBuilder()
					.where(de.duester.genealogy.model.dao.RoleCodeDao.Properties.Code
							.eq(roleCode)).list();
			if (rc.size() > 0)
				return rc.get(0);
			else
				return null;
		}
	}
}
